package contegor.scire.core;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * {@link ReturnPointObserver} that allows to wait until the {@link Task}s to its {@link ReturnPoint}
 * this is attached have finished its processing.
 *
 * @version $Revision$
 * @author  Héctor Cirbián Pousa ($Author$)
 */
@Version("$Id$")
public class ReturnPointWaitingObserver implements ReturnPointObserver {

  private CountDownLatch counter;

  public ReturnPointWaitingObserver(int numberTasks) {
    this.counter = new CountDownLatch(numberTasks);
  }

  @Override
  public void finish(TaskId taskId) {
    counter.countDown();
  }

  public void await() throws InterruptedException {
    counter.await();
  }

  public void await(long timeout, TimeUnit unit) throws InterruptedException {
    counter.await(timeout, unit);
  }
}
